const goodsClassifyModel = require('../models/goodsClassifyModel')
const create = async (ctx, next) => {
  const req = ctx.request.body;
  // 校验名称是否已经存在
  const classify = await goodsClassifyModel.findOne({
    name: req.name
  });
  if (classify) {
    ctx.body = {
      code: 0,
      msg: '名称重复！'
    }
    return;
  }
  const data = Object.assign({status: 0, createDate: new Date()}, req);
  const result = await goodsClassifyModel.create(data);
  if (result) {
    ctx.body = {
      code: 1,
      msg: '添加成功！',
      data: {}
    };
  }
}

const find = async (ctx, next) => {
  const result = await goodsClassifyModel.find({
    status: {
      $gt: 0
    }
  }, null , {
    sort: {
      sort: 'asc'
    }
  });
  if (result) {
    ctx.body = {
      code: 1,
      data: result
    };
  }
}

const update = async (ctx, next) => {
  const req = ctx.request.body;
  const result = await goodsClassifyModel.update({
    _id: req._id
  }, req);
  if (result) {
    ctx.body = {
      code: 1,
      msg: '修改成功！',
      data: {}
    };
  }
}

module.exports = {
  create,
  find,
  update
};